﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utility;
using Utility.Model;
using System.Data;
using Hxj.Data;
using Utility.Json;

namespace MsgCenter.Web.Handlers
{
    /// <summary>
    /// Handler_Role 的摘要说明
    /// </summary>
    public class Handler_Role : BaseUI.BaseHandler
    {
        /// <summary>
        /// 保存角色权限
        /// </summary>
        public void SaveRoleAuth()
        {
            string SelectAuth = ParamsOfRequest.Request("SelectAuth");
            string strType = ParamsOfRequest.Request("OptionType");
            string strRoleID = ParamsOfRequest.Request("RoleID");
            string strRoleName = ParamsOfRequest.Request("RoleName");
            string strRoleDes = ParamsOfRequest.Request("RoleDes");
            string strIsAdmin = ParamsOfRequest.Request("IsAdmin");
            try
            {
                Sys_Role model = BLL.BLLServiceHelper.GetRoleService().GetModel(strRoleID);
                if (model == null)
                {
                    model = new Sys_Role();
                    model.RoleID = strRoleID;
                    model.CreateUser = base.Account;
                }
                model.RoleName = strRoleName;
                model.RoleDes = strRoleDes;
                if (strIsAdmin == "1")
                {
                    model.IsAdmin = true;
                }
                else
                {
                    model.IsAdmin = false;
                }
                BLL.BLLServiceHelper.GetRoleService().Save(model, SelectAuth);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }

        #region 创建角色权限树
        /// <summary>
        /// 创建角色权限树
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public void CreateRoleAuth()
        {
            string reulst = string.Empty;
            string RoleID = ParamsOfRequest.Request("RoleID");
            using (DataSet ds = BLL.BLLServiceHelper.GetRoleService().GetAllList(RoleID))
            {
                DataTable moduleStr = ds.Tables[0];
                DataTable moduleTable_YX = ds.Tables[1];
                DataTable moduleOperateStr = ds.Tables[2];
                DataTable moduleOperateTable_YX = ds.Tables[3];
                TreeItem ti;
                string strEmp = Guid.Empty.ToString();
                ti = new TreeItem() { id = strEmp + "_0", text = "所有系统模块列表", value = strEmp, isexpand = true };
                JsonRoleAuthTree.GetTreeModuleItemFromDataTable(ti, moduleStr, moduleTable_YX, moduleOperateStr, moduleOperateTable_YX, "0");
                reulst = ti.ToJsonString();
                moduleStr = moduleTable_YX = moduleOperateStr = moduleOperateTable_YX = null;
            }
            Response.Write(reulst);
        }
        #endregion

        /// <summary>
        /// 获取角色列表
        /// </summary>
        public void GetRoleList()
        {
            WhereClip wcp = new WhereClip();
            string _search = ParamsOfRequest.Request("_search");
            wcp = wcp.And(Sys_Role._.IsDelete == false);
            var pageConditon = GetPageConditon(wcp);
            JsonJqGridData gridList = BLL.BLLServiceHelper.GetBaseDataService().GetGridDataTable<Sys_Role>(pageConditon);
            string strResult = gridList.ToJsonString();
            base.Response.Write(strResult);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        public void Delete()
        {
            string strID = ParamsOfRequest.Request("RoleID");
            try
            {
                BLL.BLLServiceHelper.GetRoleService().Delete(strID);
                base.GetSuccessJson();
            }
            catch (Exception ex)
            {
                base.GetFailureJson(ex.Message);
            }
        }
    }
}